var canvasWidth = 877;
var canvasHeight = 672;
var canvas = document.getElementById('myCanvas');
canvas.width = canvasWidth;
canvas.height = canvasHeight;
var context = canvas.getContext('2d');
var image = new Image();
var radius = 30;
image.src = 'images/pic.png'; //注意路径！（相当于在HTML里面写js）
image.onload = function() {
    initCanvas();
}

// 初始化画布方法
function initCanvas() {
    console.log('调用初始化画布方法')
        //清除上一次绘制
    context.clearRect(0, 0, canvas.width, canvas.height);
    context.save(); //保存！
    context.beginPath()
    var x = Math.random() * (canvas.width - 2 * radius) + radius;
    var y = Math.random() * (canvas.width - 2 * radius) + radius;
    console.log(x);
    console.log(y);
    context.arc(x, y, radius, 0, 2 * Math.PI, false)

    // 用clip()剪切圆形区域
    context.clip();
    context.drawImage(image, 0, 0);

    // 恢复保持的状态
    context.restore();
}

function reset() {
    console.log('reset');
    initCanvas();
}

function show() {
    console.log('show');
    // 初始化半径值
    radius = 2 * Math.max(canvas.width, canvas.height);
    console.log(radius);
    initCanvas();
}